package com.bh.ly.oa.adapter;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bh.ly.oa.R;
import com.bh.ly.oa.util.Config;
import com.loopj.android.image.SmartImageView;

public class LicenseListAdapter extends BaseAdapter{
	
	public List<JSONObject> getArray() {
		return array;
	}

	public void setArray(List<JSONObject> array) {
		this.array = array;
	}
	
	List<JSONObject> array = null;
	Context context = null;
	public LicenseListAdapter(Context context){
		this.context = context;
//		this.array = HttpClent.getLicenseJsonData(((LicenseActiivity)context).getparamMap());
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public Object getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LicenseItem licenseItem = null;
		if (convertView == null) {
			licenseItem = new LicenseItem();
			convertView = LayoutInflater.from(context).inflate(R.layout.license_item, null);
		} else {
			licenseItem = (LicenseItem) convertView.getTag();
		}
		
		JSONObject jsonObject = (JSONObject) array.get(position);
		licenseItem.jsonobject=jsonObject;
		initView(position,convertView,licenseItem);
		try {
			licenseItem.type.setText(jsonObject.getString("certificate_name"));
			licenseItem.company.setText(jsonObject.getString("suppliername"));
			String url = jsonObject.getString("url");
			if (url != null && url.length() > 0) {
			    licenseItem.licenseImage.setImageUrl(Config.domain + url);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		if(this.getCount() == 1)
			convertView.setBackgroundResource(R.drawable.btu_add_single);
		else if(position==0){
			convertView.setBackgroundResource(R.drawable.btu_addabove);
		}else if(position == getCount()-1){
			convertView.setBackgroundResource(R.drawable.btu_addblow);
		}else
			convertView.setBackgroundResource(R.drawable.btu_addmid);
		convertView.setPadding(10, 5, 10, 5);
		return convertView;
	}
	
	private void initView(int position,View convertView,LicenseItem licenseItem){
        
		licenseItem.type = (TextView) convertView.findViewById(R.id.license_type);
		licenseItem.company = (TextView) convertView.findViewById(R.id.license_company); 
		licenseItem.licenseImage = (SmartImageView) convertView.findViewById(R.id.license_image); 
        convertView.setTag(licenseItem);
    }
	class LicenseItem {
		TextView type;
		TextView company;
		SmartImageView licenseImage;
		JSONObject jsonobject;
	}

}
